package org.skjworld.wiki2 ;

import com.google.appengine.api.datastore.* ;

public class Database
{
	private DatastoreService datastore ;

    public Database()
    {
    	 datastore = DatastoreServiceFactory.getDatastoreService();
    }
    
    // CREATE A WIKI2 OBJECT
    public void createObject(	String kind, String ID, String type, String subType, byte [] data, 
    							String owners, String collaborators, String viewers,
    							String versions,
    							String verificationScore, String popularityScore,
    							String [] links)
    {
    	try
    	{
    		// OBJECT DEFINITION
    		Entity entity = new Entity(kind,ID) ;
    		entity.setProperty("TYPE", type) ;
    		entity.setProperty("SUB_TYPE", subType) ;
    		entity.setProperty("DATA", new Blob(data)) ;
    		
    		// OBJECT OWNERSHIP
    		entity.setProperty("OWNERS", owners) ;
    		entity.setProperty("COLLABORATORS", collaborators) ;
    		entity.setProperty("VIEWERS", viewers) ;
    		
    		// OBJECT VERSIONING
    		entity.setProperty("VERSIONS", versions) ;
    		
    		// OBJECT QUALITY
    		entity.setProperty("VERSIONS", verificationScore) ;
    		entity.setProperty("VERSIONS", popularityScore) ;
    		
    		// Setting the links
    		for(int i = 0; i < links.length;i += 2)
    		{
    			entity.setProperty(links[i], links[i+1]) ;
    		}
    		
    		datastore.put(entity) ;
    	}
    	catch(Exception e)
    	{
    		e.printStackTrace() ;
    	}
    }
    
    // RETRIEVE A WIKI2 OBJECT
    public Entity retrieveObject(String kind, String ID)
    {
    	try
    	{
    		Entity entity = datastore.get(KeyFactory.createKey(kind, ID));
    		return entity ;
    	}
    	catch(Exception e)
    	{
    		e.printStackTrace() ;
    	}
    	
    	return null ;
    }
    
    // RETRIEVE A WIKI2 OBJECT's LINK
    public Object retrieveLink(String kind, String ID,String link)
    {
    	try
    	{
    		Entity entity = datastore.get(KeyFactory.createKey(kind, ID));
    		return entity.getProperty(link) ;
    	}
    	catch(Exception e)
    	{
    		e.printStackTrace() ;
    	}
    	
    	return null ;
    }
    
    // UPDATE A WIKI2 OBJECT's LINK
    public void updateLink(String kind, String ID, String link, Object linkObject)
    {
    	try
    	{
    		Entity entity = datastore.get(KeyFactory.createKey(kind, ID));
    		entity.setProperty(link, linkObject) ;
    		datastore.put(entity) ;
    	}
    	catch(Exception e)
    	{
    		e.printStackTrace() ;
    	}
    }
    
    // DELETE A WIKI2 OBJECT
    public void deleteObject(String kind, String ID)
    {
    	try
    	{
    		Key key = KeyFactory.createKey("Resource", ID);
    		datastore.delete(key) ;
    	}
    	catch(Exception e)
    	{
    		e.printStackTrace() ;
    	}
    }
    
    // DELETE A WIKI2 OBJECT's LINK
    public void deleteLink(String kind, String ID, String link)
    {
    	try
    	{
    		Entity entity = datastore.get(KeyFactory.createKey(kind, ID));
    		entity.removeProperty(link) ;
    		datastore.put(entity) ;
    	}
    	catch(Exception e)
    	{
    		e.printStackTrace() ;
    	}
    }
}

/*	createObject(	String kind, String ID, String type, String subType, byte [] data, 
					String owners, String collaborators, String viewers,
					String versions,
					String verificationScore, String popularityScore,
					String [] links)
					
	retrieveObject(String kind, String ID)
	retrieveLink(String kind, String ID,String link)
	updateLink(String kind, String ID, String link, Object linkObject)
	deleteObject(String kind, String ID)
	deleteLink(String kind, String ID, String link)
	
*/